/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import io from '@/module/io'
import cookies from 'js-cookie'
let groupList = [
  {
    id: '1',
    userGroup: '用户组1',
    remark: 'XXX1',
    status: 1,
    userIds: '1,2',
    userAdmin: '柏林1、小小2',
    resourceGroup: '资源池1、资源池2',
    resourceIds: '1,3'
  },
  {
    id: '2',
    userGroup: '用户组2',
    remark: 'XXX2',
    status: 0,
    userIds: '1,2',
    userAdmin: '柏林1、小小2',
    resourceGroup: '资源池1、资源池2',
    resourceIds: '1,2'
  },
  {
    id: '3',
    userGroup: '用户组3',
    remark: 'XXX3',
    status: 1,
    userIds: '1,2',
    userAdmin: '柏林1、小小2',
    resourceGroup: '资源池1、资源池2',
    resourceIds: '1,3'
  },
  {
    id: '4',
    userGroup: '用户组4',
    remark: 'XXX4',
    status: 0,
    userIds: '1,2',
    userAdmin: '柏林1、小小2',
    resourceGroup: '资源池1、资源池2',
    resourceIds: '1,3'
  },
  {
    id: '5',
    userGroup: '用户组5',
    remark: 'XXX5',
    status: 0,
    userIds: '1,2',
    userAdmin: '柏林1、小小2',
    resourceGroup: '资源池1、资源池2',
    resourceIds: '1,3'
  },
  {
    id: '6',
    userGroup: '用户组6',
    remark: 'XXX6',
    status: 0,
    userIds: '1,2',
    userAdmin: '柏林1、小小2',
    resourceGroup: '资源池1、资源池2',
    resourceIds: '1,3'
  },
  {
    id: '7',
    userGroup: '用户组7',
    remark: 'XXX7',
    status: 0,
    userIds: '1,2',
    userAdmin: '柏林1、小小2',
    resourceGroup: '资源池1、资源池2',
    resourceIds: '1,3'
  },
  {
    id: '8',
    userGroup: '用户组8',
    remark: 'XXX8',
    status: 0,
    userIds: '1,2',
    userAdmin: '柏林1、小小2',
    resourceGroup: '资源池1、资源池2',
    resourceIds: '1,3'
  },
  {
    id: '9',
    userGroup: '用户组9',
    remark: 'XXX9',
    status: 0,
    userIds: '1,2',
    userAdmin: '柏林1、小小2',
    resourceGroup: '资源池1、资源池2',
    resourceIds: '1,3'
  },
  {
    id: '10',
    userGroup: '用户组10',
    remark: 'XXX10',
    status: 0,
    userIds: '1,2',
    userAdmin: '柏林1、小小2',
    resourceGroup: '资源池1、资源池2',
    resourceIds: '1,3'
  },
  {
    id: '11',
    userGroup: '用户组11',
    remark: 'XXX11',
    status: 0,
    userIds: '1,2',
    userAdmin: '柏林1、小小2',
    resourceGroup: '资源池1、资源池2',
    resourceIds: '1,3'
  },
  {
    id: '12',
    userGroup: '用户组12',
    remark: 'XXX12',
    status: 0,
    userIds: '1,2',
    userAdmin: '柏林1、小小2',
    resourceGroup: '资源池1、资源池2',
    resourceIds: '1,3'
  },
  {
    id: '13',
    userGroup: '用户组13',
    remark: 'XXX13',
    status: 0,
    userIds: '1,2',
    userAdmin: '柏林1、小小2',
    resourceGroup: '资源池1、资源池2',
    resourceIds: '1,3'
  },
  {
    id: '14',
    userGroup: '用户组14',
    remark: 'XXX14',
    status: 0,
    userIds: '1,2',
    userAdmin: '柏林1、小小2',
    resourceGroup: '资源池1、资源池2',
    resourceIds: '1,3'
  },
  {
    id: '15',
    userGroup: '用户组15',
    remark: 'XXX15',
    status: 0,
    userIds: '1,2',
    userAdmin: '柏林1、小小2',
    resourceGroup: '资源池1、资源池2',
    resourceIds: '1,3'
  },
  {
    id: '16',
    userGroup: '用户组16',
    remark: 'XXX16',
    status: 0,
    userIds: '1,2',
    userAdmin: '柏林1、小小2',
    resourceGroup: '资源池1、资源池2',
    resourceIds: '1,3'
  },
  {
    id: '17',
    userGroup: '用户组17',
    remark: 'XXX17',
    status: 0,
    userIds: '1,2',
    userAdmin: '柏林1、小小2',
    resourceGroup: '资源池1、资源池2',
    resourceIds: '1,3'
  },
  {
    id: '18',
    userGroup: '用户组18',
    remark: 'XXX18',
    status: 0,
    userIds: '1,2',
    userAdmin: '柏林1、小小2',
    resourceGroup: '资源池1、资源池2',
    resourceIds: '1,3'
  }
]
export default {
  /**
   * get userInfo
   */
  getUserInfo ({ state }, payload) {
    return new Promise((resolve, reject) => {
      io.get('users/get-user-info', payload, res => {
        state.userInfo = res.data
        resolve(res.data)
      }).catch(e => {
        reject(e)
      })
    })
  },
  /**
   * sign out
   */
  signOut () {
    io.post('signOut', res => {
      setTimeout(() => {
        window.localStorage.clear()
        window.sessionStorage.clear()
        cookies.remove('sessionId')
        window.location.href = `${window.SITE_URL}/login.html`
      }, 100)
    }).catch(e => {
      console.log(e)
    })
  },
  /**
   * get token list
   * User loginUser,
   * Integer pageNo,
   * String searchVal,
   * Integer pageSize
   */
  getTokenListP ({ state }, payload) {
    return new Promise((resolve, reject) => {
      io.get('access-tokens', payload, res => {
        resolve(res.data)
      }).catch(e => {
        reject(e)
      })
    })
  },
  /**
   * create token
   * User loginUser,
   * int userId,
   * String expireTime,
   * String token
   */
  createToken ({ state }, payload) {
    return new Promise((resolve, reject) => {
      io.post('access-tokens', payload, res => {
        resolve(res)
      }).catch(e => {
        reject(e)
      })
    })
  },
  /**
   * update token
   * User loginUser,
   * int userId,
   * String expireTime,
   * String token
   */
  updateToken ({ state }, payload) {
    return new Promise((resolve, reject) => {
      io.put(`access-tokens/${payload.id}`, payload, res => {
        resolve(res)
      }).catch(e => {
        reject(e)
      })
    })
  },
  /**
   * create token
   * User loginUser,
   * int userId,
   * String expireTime
   */
  generateToken ({ state }, payload) {
    return new Promise((resolve, reject) => {
      io.post('access-tokens/generate', payload, res => {
        resolve(res.data)
      }).catch(e => {
        reject(e)
      })
    })
  },
  /**
   * delete token
   * User loginUser,
   * int  id
   */
  deleteToken ({ state }, payload) {
    return new Promise((resolve, reject) => {
      io.delete(`access-tokens/${payload.id}`, payload, res => {
        resolve(res)
      }).catch(e => {
        reject(e)
      })
    })
  },
  /**
   * get UserGroupList
   */
  getUserGroupList (context, payload) {
    return setTimeout(() => {
      context.commit('setUserGroupList', groupList)
      groupList.length > 0 && context.commit('setCurrentGroup', groupList[0])
    }, 500)
  },
  deleteUserGroupById (context, payload) {
    let index = groupList.findIndex(v => v.id === payload)
    if (index !== -1) {
      groupList.splice(index, 1)
      context.dispatch('getUserGroupList')
    }
  }
}
